package com.xxl.job.executor.process;


import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.SpringContextUtil;
import com.xxl.job.executor.communication.TcpClient;
import com.xxl.job.executor.constant.FireConstant;
import com.xxl.job.executor.model.GatherDefModel;
import com.xxl.job.executor.model.SignalDefDhModel;
import com.xxl.job.executor.model.SignalDefHkModel;
import com.xxl.job.executor.model.SignalDefXfModel;
import com.xxl.job.executor.protocol.TCPProtocol;
import com.xxl.job.executor.protocol.XfTCPProtocol;
import com.xxl.job.executor.service.AutomaticControlService;
import com.xxl.job.executor.service.FireCollectService;
import com.xxl.job.executor.service.GatherDefService;
import com.xxl.job.executor.utils.CRCUtil;
import com.xxl.job.executor.utils.DateUtil;
import com.xxl.job.executor.utils.TransUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by huanggch on 2019/2/28.
 */
public class FireProcess {

    private XfTCPProtocol tcpClient;

    private String param;

    private String ip;

    private int port;

    public FireProcess(String param) {
        this.param = param;
    }

    public void collectProcess() throws Exception{
//        while (true) {
            GatherDefModel gatherDefModel = SpringContextUtil.getBean(GatherDefService.class).getGatherDefModel(param);
            if (gatherDefModel == null) {
                XxlJobLogger.log("消防系统 ：未获取到采集口配置信息");
                return;
            }
            ip =gatherDefModel.getCollectIp();
            port =Integer.parseInt(gatherDefModel.getCollectPort());

            System.out.println("---------------------------------------------------");
            System.out.println(ip);
            System.out.println(port);

            tcpClient = new XfTCPProtocol(ip, port,gatherDefModel.getGatherId(),gatherDefModel.getResId());
//                try{
            tcpClient.sever();
//                }catch(IOException e){
////                    XxlJobLogger.log("消防系统：client创建连接失败，等待10分钟重连...，ip: " + ip + " ,port: " + port);
////                    System.out.println("消防系统：client创建连接失败，等待10分钟重连...，ip: " + ip + " ,port: " + port);
////                    TimeUnit.SECONDS.sleep(600);
////                }
//        }
    }


    /**
     * 校验和
     *
     * @param hexdata
     * @return
     */
    public static boolean check2binaryString(String hexdata) {
        String substr = hexdata.substring(4, hexdata.length() - 6);//控制单元中各字节数据(第3～27字节)及应用数据单元数据
        String checkSum = hexdata.substring(hexdata.length() - 6, hexdata.length() - 4);//校验和(1字节)
        String sum = TransUtils.makeChecksums(substr).toUpperCase();//求和
        if (sum.equals(checkSum)) {
            return true;
        }
        return false;
    }

    /**
     * 应答命令
     */
    public static String answerCommand(String Serial, String commandStr) {
        //String Serial = hexdata.substring(4,2);
        String str = Serial +  //业务流水号
                "0102" + //协议版本号
                DateUtil.hexDateString() + //时间标签
                "50F80B540200" +  //源地址
                "500000000000" + //目的地址
                "0000" +     //应用数据单元长度
                commandStr;    //命令字节
        String answerStr = "4040" + str + TransUtils.makeChecksums(str) + "2323";
        return answerStr;
    }

    /**
     * 解析部件地址
     **/
    public String addressParse(String componentAddr) {
        return String.valueOf(Integer.parseInt(componentAddr.substring(6, 8) + "" +
                componentAddr.substring(4, 6) + "" +
                componentAddr.substring(2, 4) + "" +
                componentAddr.substring(0, 2), 16));
    }
}
